package com.xbongbong.pro.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.pojo.dto.DefaultComboValueListDTO;
import com.xbongbong.pro.statistic.pojo.dto.FundAccountListDTO;
import com.xbongbong.pro.statistic.pojo.vo.DefaultComboValueListVO;
import com.xbongbong.pro.statistic.pojo.vo.FundAccountListVO;
import com.xbongbong.pro.statistic.service.ChartFilterService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 图表中心筛选Controller
 *
 * @author 徐俊杰
 * @version v1.0
 * @date 2020/3/17 16:51
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/chart/filter")
public class ChartFilterController {

    @Resource
    private ChartFilterService chartFilterService;

    /**
     * 获取已开启的资金账户列表
     *
     * @param fundAccountListDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2020/3/11 17:02
     * @since v1.0
     */
    @PostMapping(value = "/fundAccount/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String homeList(@RequestBody @Valid FundAccountListDTO fundAccountListDTO, BindingResult br) {
        XbbResponse<FundAccountListVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundAccountListVO fundAccountListVO = chartFilterService.fundAccountList(fundAccountListDTO);
                response = new XbbResponse<>(fundAccountListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * 获取统计这边查询时，下拉选的默认值
     *
     * @param defaultComboValueListDTO
     * @param br
     * @return
     */
    @PostMapping(value = "/defaultComboValue/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String defaultComboValueList(@RequestBody @Valid DefaultComboValueListDTO defaultComboValueListDTO, BindingResult br) {
        XbbResponse<DefaultComboValueListVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DefaultComboValueListVO defaultComboValueList = chartFilterService.defaultComboValueList(defaultComboValueListDTO);
                response = new XbbResponse<>(defaultComboValueList);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
